package ru.slovoslovo.cropmotion;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlRootElement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@XmlRootElement 
public class CropSettings extends JAXBSerializer{
    
    public static final String LOCATION = "./settings.xml";
    
    private static final Logger LOG = LoggerFactory.getLogger(CropSettings.class);
    
    public CropSettings() {
        super(CropSettings.class);
    }

    private DestinationMode destinationMode = DestinationMode.SPECIFIED_PATH;
    
    private TemplateApplyMode templateApplyMode = TemplateApplyMode.RANDOM;
    
    private String sourcePath = "";
    
    private String destinationPath = "";
    
    private String subfolderPath = "";

    public DestinationMode getDestinationMode() {
        return destinationMode;
    }

    public void setDestinationMode(DestinationMode destinationMode) {
        this.destinationMode = destinationMode;
    }

    public TemplateApplyMode getTemplateApplyMode() {
        return templateApplyMode;
    }

    public void setTemplateApplyMode(TemplateApplyMode templateApplyMode) {
        this.templateApplyMode = templateApplyMode;
    }

    public String getSourcePath() {
        return sourcePath;
    }

    public void setSourcePath(String sourcePath) {
        this.sourcePath = sourcePath;
    }

    public String getDestinationPath() {
        return destinationPath;
    }

    public void setDestinationPath(String destinationPath) {
        this.destinationPath = destinationPath;
    }

    public String getSubfolderPath() {
        return subfolderPath;
    }

    public void setSubfolderPath(String subfolderPath) {
        this.subfolderPath = subfolderPath;
    }

    /**
     * Deserialize settings from source file
     * 
     * @param source file path
     * @return deserialized settings if success, null otherwise
     */
    public CropSettings deserialize(String source) {
        CropSettings result = null;
        
        try {
            result = (CropSettings) getJaxbUnmarshaller().unmarshal(new FileInputStream(source));
        } catch (FileNotFoundException e) {
            LOG.error("Error deserializing settings, source file not found" + e.getMessage());
        } catch (JAXBException e) {
            LOG.error("Error deserializing settings" + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * Serialize settings to destination file
     * 
     * @param destiantion file path
     * @return operation result true/false
     */
    public boolean serialize(String destination) {
        boolean result = false;
        
        if (getJaxbMarshaller() != null){
            try {
                getJaxbMarshaller().marshal(this, new FileOutputStream(destination));
                result = true;
            } catch (FileNotFoundException e) {
                LOG.error("Error serializing settings, destination file not found" + e.getMessage());
            } catch (JAXBException e) {
                LOG.error("Error serializing settings" + e.getMessage());
            }
        }
        
        return result;
    }
    
}
